﻿/// <reference path="PerformanceTesterBase.ts" />
/// <reference path="..\TsLinq.d.ts" />
/// <reference path="..\KeyValuePair.ts" />
/// <reference path="..\Dictionary.ts" />
/// <reference path="..\Linq.ts" />
/// <reference path="..\..\Scripts\linq.js.d.ts" />
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var LinqTester = (function (_super) {
    __extends(LinqTester, _super);
    function LinqTester(logger) {
        _super.call(this, logger);
        this.bigArray = [];
        for (var i = 0; i <= 10000000; i++) {
            this.bigArray.push(i);
        }
        this.bigNumbers = new Linq(this.bigArray);
        this.bigLinqJs = Enumerable.from(this.bigArray);
    }
    LinqTester.prototype.linq_where = function () {
        var _this = this;
        this.testPerformance('where(x%2==0)', function () {
            return _this.bigNumbers.where(function (bn) {
                return bn % 2 == 0;
            }).count();
        }, function () {
            return _this.bigLinqJs.where(function (x) {
                return x % 2 == 0;
            }).count();
        });
    };

    LinqTester.prototype.linq_single = function () {
        var _this = this;
        this.testPerformance('single(x == 10,000,000)', function () {
            return _this.bigNumbers.single(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.single(function (x) {
                return x == 10000000;
            });
        });
        this.testPerformance('single(x == 0)', function () {
            return _this.bigNumbers.single(function (bn) {
                return bn == 0;
            });
        }, function () {
            return _this.bigLinqJs.single(function (x) {
                return x == 0;
            });
        });
    };

    LinqTester.prototype.linq_singleOrDefault = function () {
        var _this = this;
        this.testPerformance('singleOrDefault(x==10,000,000)', function () {
            return _this.bigNumbers.singleOrDefault(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.singleOrDefault(function (x) {
                return x == 10000000;
            });
        });
        this.testPerformance('singleOrDefault(x==0)', function () {
            return _this.bigNumbers.singleOrDefault(function (bn) {
                return bn == 0;
            });
        }, function () {
            return _this.bigLinqJs.singleOrDefault(function (x) {
                return x == 0;
            });
        });
    };

    LinqTester.prototype.linq_all = function () {
        var _this = this;
        this.testPerformance('all(x<10,000,000)', function () {
            return _this.bigNumbers.all(function (bn) {
                return bn < 10000000;
            });
        }, function () {
            return _this.bigLinqJs.all(function (x) {
                return x < 10000000;
            });
        });
        this.testPerformance('all(x<5,000,000)', function () {
            return _this.bigNumbers.all(function (bn) {
                return bn < 5000000;
            });
        }, function () {
            return _this.bigLinqJs.all(function (x) {
                return x < 5000000;
            });
        });
    };

    LinqTester.prototype.linq_first = function () {
        var _this = this;
        this.testPerformance('first(x==10,000,000)', function () {
            return _this.bigNumbers.first(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.first(function (x) {
                return x == 10000000;
            });
        });
        this.testPerformance('first(x==0)', function () {
            return _this.bigNumbers.first(function (bn) {
                return bn == 0;
            });
        }, function () {
            return _this.bigLinqJs.first(function (x) {
                return x == 0;
            });
        });
    };

    LinqTester.prototype.linq_firstOrDefault = function () {
        var _this = this;
        this.testPerformance('firstOrDefault(x==10,000,000)', function () {
            return _this.bigNumbers.firstOrDefault(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.firstOrDefault(function (x) {
                return x == 10000000;
            });
        });
        this.testPerformance('firstOrDefault(x==0)', function () {
            return _this.bigNumbers.firstOrDefault(function (bn) {
                return bn == 0;
            });
        }, function () {
            return _this.bigLinqJs.firstOrDefault(function (x) {
                return x == 0;
            });
        });
    };

    LinqTester.prototype.linq_last = function () {
        var _this = this;
        this.testPerformance('last(x==10,000,000)', function () {
            return _this.bigNumbers.last(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.last(function (x) {
                return x == 10000000;
            });
        });
        this.testPerformance('last(x==0)', function () {
            return _this.bigNumbers.last(function (bn) {
                return bn == 0;
            });
        }, function () {
            return _this.bigLinqJs.last(function (x) {
                return x == 0;
            });
        });
    };

    LinqTester.prototype.linq_lastOrDefault = function () {
        var _this = this;
        this.testPerformance('lastOrDefault(x==10,000,000)', function () {
            return _this.bigNumbers.lastOrDefault(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.lastOrDefault(function (x) {
                return x == 10000000;
            });
        });
        this.testPerformance('lastOrDefault(x==0)', function () {
            return _this.bigNumbers.lastOrDefault(function (bn) {
                return bn == 0;
            });
        }, function () {
            return _this.bigLinqJs.lastOrDefault(function (x) {
                return x == 0;
            });
        });
    };

    LinqTester.prototype.linq_select = function () {
        var _this = this;
        var counter = 0;
        this.testPerformance('select({value:x})', function () {
            return _this.bigNumbers.select(function (n) {
                return { value: n };
            });
        }, function () {
            return _this.bigLinqJs.select(function (n) {
                return { value: n };
            });
        }, function (val) {
            return val.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_indexOf = function () {
        var _this = this;
        this.testPerformance('indexOf(10,000,000)', function () {
            return _this.bigNumbers.indexOf(10000000);
        }, function () {
            return _this.bigLinqJs.indexOf(function (x) {
                return x == 10000000;
            });
        });
    };

    LinqTester.prototype.linq_elementAt = function () {
        var _this = this;
        this.testPerformance('elementAt(10,000,000)', function () {
            return _this.bigNumbers.elementAt(10000000);
        }, function () {
            return _this.bigLinqJs.elementAt(10000000);
        });
    };

    LinqTester.prototype.linq_elementAtOrDefault = function () {
        var _this = this;
        this.testPerformance('elementAtOrDefault(10,000,000)', function () {
            return _this.bigNumbers.elementAtOrDefault(10000000);
        }, function () {
            return _this.bigLinqJs.elementAtOrDefault(10000000);
        });
    };

    LinqTester.prototype.linq_forEach = function () {
        var _this = this;
        var todo = function (item) {
        };
        this.testPerformance('forEach({})', function () {
            return _this.bigNumbers.forEach(todo);
        }, function () {
            return _this.bigLinqJs.forEach(todo);
        });
    };

    LinqTester.prototype.linq_contains = function () {
        var _this = this;
        this.testPerformance('indexOf(10,000,000)', function () {
            return _this.bigNumbers.contains(10000000);
        }, function () {
            return _this.bigLinqJs.contains(10000000, function (x) {
                return x;
            });
        });
    };

    LinqTester.prototype.linq_any = function () {
        var _this = this;
        this.testPerformance('any(x==10,000,000)', function () {
            return _this.bigNumbers.any(function (bn) {
                return bn == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.any(function (x) {
                return x == 10000000;
            });
        });
    };

    LinqTester.prototype.linq_count = function () {
        var _this = this;
        this.testPerformance('count(x==10000000)', function () {
            return _this.bigNumbers.count(function (x) {
                return x == 10000000;
            });
        }, function () {
            return _this.bigLinqJs.count(function (x) {
                return x == 10000000;
            });
        });
    };

    LinqTester.prototype.linq_toDictionary = function () {
        var _this = this;
        this.testPerformance('toDictionary(x => x % 2)', function () {
            return _this.bigNumbers.toDictionary(function (x) {
                return x % 2;
            });
        }, function () {
            return _this.bigLinqJs.toDictionary(function (x) {
                return x % 2;
            });
        }, function (val) {
            return val.count() + ' keys generated';
        });
    };

    LinqTester.prototype.linq_groupBy = function () {
        var _this = this;
        this.testPerformance('groupBy(x => x % 2)', function () {
            return _this.bigNumbers.groupBy(function (x) {
                return x % 2;
            });
        }, function () {
            return _this.bigLinqJs.groupBy(function (x) {
                return x % 2;
            });
        }, function (val) {
            if (val === undefined) {
                return '';
            }
            return val.count() + ' keys generated';
        });
    };

    LinqTester.prototype.linq_sum = function () {
        var takeArray = this.bigNumbers.take(1000000);
        this.testPerformance('sum(first 100000 entries) DOES NOT WORK IN LINQ.JS AS EXPECTED', function () {
            return takeArray.sum();
        }, function () {
            return Enumerable.from(takeArray).sum();
        });
        this.testPerformance('sum(first 100000 entries * 2) DOES NOT WORK IN LINQ.JS AS EXPECTED', function () {
            return takeArray.sum(function (x) {
                return x * 2;
            });
        }, function () {
            return Enumerable.from(takeArray).sum(function (x) {
                return x * 2;
            });
        });
    };

    LinqTester.prototype.linq_selectMany = function () {
        var take = [{ values: this.bigNumbers.toArray() }, { values: this.bigNumbers.toArray() }];
        this.testPerformance('selectMany(2 objects with 10000001 values in property values)', function () {
            return new Linq(take).selectMany(function (v) {
                return v.values;
            });
        }, function () {
            return Enumerable.from(take).selectMany(function (v) {
                return v.values;
            });
        }, function (val) {
            return val.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_cast = function () {
        this._logger.debug('cast<>() was not implemented by Linq.js');
    };

    LinqTester.prototype.linq_aggregate = function () {
        var _this = this;
        this.testPerformance('aggregate((n1, n2) => n1 + n2)', function () {
            return _this.bigNumbers.aggregate(function (n1, n2) {
                return n1 + n2;
            });
        }, function () {
            return _this.bigLinqJs.aggregate(function (n1, n2) {
                return n1 + n2;
            });
        });
    };

    LinqTester.prototype.linq_skip = function () {
        var _this = this;
        this.testPerformance('skip(1,000,000)', function () {
            return _this.bigNumbers.skip(1000000);
        }, function () {
            return _this.bigLinqJs.skip(1000000);
        }, function (val) {
            return val.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_take = function () {
        var _this = this;
        this.testPerformance('take(10,000,000)', function () {
            return _this.bigNumbers.take(10000000);
        }, function () {
            return _this.bigLinqJs.take(10000000);
        }, function (val) {
            return val.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_intersect = function () {
        var intersectWith = this.bigNumbers.take(10000).toArray();
        this.testPerformance('intersect(2 lists á 10000 entries)', function () {
            return new Linq(intersectWith).intersect(intersectWith);
        }, function () {
            return Enumerable.from(intersectWith).intersect(intersectWith);
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_except = function () {
        var exceptWith = this.bigNumbers.take(10000).toArray();
        this.testPerformance('except(2 lists á 10000 entries)', function () {
            return new Linq(exceptWith).except(exceptWith);
        }, function () {
            return Enumerable.from(exceptWith).except(exceptWith);
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_concat = function () {
        var _this = this;
        var concatWith = this.bigNumbers.take(10000).toArray();
        this.testPerformance('concat(with 10000 entries)', function () {
            return _this.bigNumbers.concat(concatWith);
        }, function () {
            return _this.bigLinqJs.concat(concatWith);
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_reverse = function () {
        var _this = this;
        this.testPerformance('reverse()', function () {
            return _this.bigNumbers.reverse();
        }, function () {
            return _this.bigLinqJs.reverse();
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_zip = function () {
        var _this = this;
        this.testPerformance('zip(addition of a collection of the same content)', function () {
            return _this.bigNumbers.zip(_this.bigArray, function (i1, i2) {
                return i1 + i2;
            });
        }, function () {
            return _this.bigLinqJs.zip(_this.bigArray, function (i1, i2) {
                return i1 + i2;
            });
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_min = function () {
        var _this = this;
        this.testPerformance('min()', function () {
            return _this.bigNumbers.min();
        }, function () {
            return _this.bigLinqJs.min();
        });
        this.testPerformance('min(x*2)', function () {
            return _this.bigNumbers.min(function (x) {
                return x * 2;
            });
        }, function () {
            return _this.bigLinqJs.min(function (x) {
                return x * 2;
            });
        });
    };

    LinqTester.prototype.linq_max = function () {
        var _this = this;
        this.testPerformance('max()', function () {
            return _this.bigNumbers.max();
        }, function () {
            return _this.bigLinqJs.max();
        });
        this.testPerformance('max(x*2)', function () {
            return _this.bigNumbers.max(function (x) {
                return x * 2;
            });
        }, function () {
            return _this.bigLinqJs.max(function (x) {
                return x * 2;
            });
        });
    };

    LinqTester.prototype.linq_distinct = function () {
        var arr = this.bigNumbers.select(function (x) {
            return x % 2;
        }).toArray();
        this.testPerformance('distinct(modulo from 2 of all items)', function () {
            return new Linq(arr).distinct();
        }, function () {
            return Enumerable.from(arr).distinct();
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };

    LinqTester.prototype.linq_union = function () {
        var arr = this.bigNumbers.take(10000).toArray();
        var tsLinq = new Linq(arr);
        var linqJs = Enumerable.from(arr);
        this.testPerformance('union(2 arrays á 10000 entries)', function () {
            return tsLinq.union(arr);
        }, function () {
            return linqJs.union(arr);
        }, function (vals) {
            return vals.count() + ' entries';
        });
    };
    return LinqTester;
})(PerformanceTesterBase);
